
class com.mochimedia.ui.Tagtip {    
    private static var _mc:MovieClip;
    private static var _container:MovieClip;
    private static var _width:Number = 150;
    
    private static var _tipFormat:TextFormat;
    private static var _tipText:TextField;
    
    private static var cornerRadius:Number;
    private static var arrowRadius:Number;
    private static var padding:Number;
    
    private static var bkgdColor:Number;
    private static var borderColor:Number;
    private static var borderThickness:Number;
    private static var border:Boolean = false;
    private static var textColor:Number;
    
    private static var showMaxTime:Number = 1500;
    private static var showTime:Number = 0;
    private static var fontName:String = "Verdana";
    private static var fontSize:Number = 11;
    private static var embed:Boolean = false;
    private static var heightOffset:Number;
    
    public static var showing:Boolean = false;

    //
    //
    public static function initialize (container:MovieClip):Void {
        
        if (! container["___tooltip"] ) {
            
            _container = container;
            
            cornerRadius = 4;
            arrowRadius = 10;
            padding = 4;

            heightOffset = -4;
            bkgdColor = 0x333333;
            border = true;
            borderColor = 0x666666;
            textColor = 0xffffff;
            borderThickness = 2;
            
            _mc = _container.createEmptyMovieClip("___tooltip", _container.getNextHighestDepth());
            
            makeTextField();
            
            _mc._visible = false;

        }
        
    }
    
    public static function destroy():Void {
        _mc.removeMovieClip();
    }

    //
    //
    // RENDERTEXT creates a textfield for a text node
    private static function makeTextField ():Void {

        _tipFormat = new TextFormat();
        _tipFormat.align = "center";
        _tipFormat.font = fontName;
        _tipFormat.size = fontSize;
        _tipFormat.color = textColor;
        
        _mc.createTextField("_tipText", 3, 0, 0, _width, _tipFormat.size);
        _tipText = _mc._tipText;
        _tipText.embedFonts = embed;
        _tipText.selectable = false;
        _tipText.multiline = true;
        _tipText.wordWrap = true;
        _tipText.autoSize = "center";
        _tipText.text = "";
        
        _tipText.setTextFormat(_tipFormat);

    }

    //
    //
    // DRAWTAG draws the text tag for the point
    private static function drawTag ():Void {
        if (!_mc) return;

        var textOffset:Number = _tipText._y - 2;
        var textWidth:Number = _tipText._width + padding + 10;
        var textHeight:Number = _tipText._height + padding;
        var iconWidth:Number = arrowRadius;
        var iconHeight:Number = arrowRadius;
        var xOffset:Number = Math.max(0 - (_root._xmouse - 20) + _tipText._width / 2,Math.min(0,(Stage.width) - (_root._xmouse + 20) - _tipText._width / 2));

        _tipText._x = xOffset - _tipText._width / 2;

        if (_mc.bkgd == null) {
            _mc.createEmptyMovieClip("bshadow",1);
            _mc.createEmptyMovieClip("bkgd",2);
        } else {
            _mc.bkgd.clear();
            _mc.bshadow.clear();
        }

        _mc.bkgd.beginFill(bkgdColor,100);
            
        if (border == true) {
            _mc.bkgd.lineStyle(borderThickness, borderColor, 100, true);
        }
        
        _mc.bkgd.moveTo(xOffset + 0 - textWidth / 2 + cornerRadius, textOffset)
        _mc.bkgd.lineTo(xOffset + textWidth / 2 - cornerRadius, textOffset);
        _mc.bkgd.curveTo(xOffset + textWidth / 2, textOffset, xOffset + textWidth / 2, textOffset + cornerRadius);
        _mc.bkgd.lineTo(xOffset + textWidth / 2, textOffset + cornerRadius);
        _mc.bkgd.lineTo(xOffset + textWidth / 2, textOffset + textHeight - cornerRadius);
        _mc.bkgd.curveTo(xOffset + textWidth / 2, textOffset + textHeight, xOffset + textWidth / 2 - cornerRadius, textOffset + textHeight);
        _mc.bkgd.lineTo(xOffset + textWidth / 2 - cornerRadius, textOffset + textHeight);
            
        // arrow
        _mc.bkgd.lineTo(iconWidth / 2, textOffset + textHeight);
        _mc.bkgd.lineTo(0, 0);
        _mc.bkgd.lineTo(0 - iconWidth / 2, textOffset + textHeight);
             
        _mc.bkgd.lineTo(xOffset + 0 - textWidth / 2 + cornerRadius, textOffset + textHeight);
        _mc.bkgd.curveTo(xOffset + 0 - textWidth / 2, textOffset + textHeight, xOffset + 0 - textWidth / 2, textOffset + textHeight - cornerRadius);
        _mc.bkgd.lineTo(xOffset + 0 - textWidth / 2, textOffset + textHeight - cornerRadius);
        _mc.bkgd.lineTo(xOffset + 0 - textWidth / 2, textOffset + cornerRadius);
        _mc.bkgd.curveTo(xOffset + 0 - textWidth / 2, textOffset, xOffset + 0 - textWidth / 2 + cornerRadius, textOffset);
        _mc.bkgd.lineTo(xOffset + 0 - textWidth / 2 + cornerRadius, textOffset);
            
        _mc.bkgd.endFill();

        // draw shadow
        _mc.bshadow._x = _mc.bshadow._y = 4;


        _mc.bshadow.beginFill(0x000000, 10);
            
        _mc.bshadow.moveTo(xOffset + 0 - textWidth / 2 + cornerRadius, textOffset)
        _mc.bshadow.lineTo(xOffset + textWidth / 2 - cornerRadius, textOffset);
        _mc.bshadow.curveTo(xOffset + textWidth / 2, textOffset, xOffset + textWidth / 2, textOffset + cornerRadius);
        _mc.bshadow.lineTo(xOffset + textWidth / 2, textOffset + cornerRadius);
        _mc.bshadow.lineTo(xOffset + textWidth / 2, textOffset + textHeight - cornerRadius);
        _mc.bshadow.curveTo(xOffset + textWidth / 2, textOffset + textHeight, xOffset + textWidth / 2 - cornerRadius, textOffset + textHeight);
        _mc.bshadow.lineTo(xOffset + textWidth / 2 - cornerRadius, textOffset + textHeight);
            
        // arrow
        _mc.bshadow.lineTo(iconWidth / 2, textOffset + textHeight);
        _mc.bshadow.lineTo(0, 0);
        _mc.bshadow.lineTo(0 - iconWidth / 2, textOffset + textHeight);
             
        _mc.bshadow.lineTo(xOffset + 0 - textWidth / 2 + cornerRadius, textOffset + textHeight);
        _mc.bshadow.curveTo(xOffset + 0 - textWidth / 2, textOffset + textHeight, xOffset + 0 - textWidth / 2, textOffset + textHeight - cornerRadius);
        _mc.bshadow.lineTo(xOffset + 0 - textWidth / 2, textOffset + textHeight - cornerRadius);
        _mc.bshadow.lineTo(xOffset + 0 - textWidth / 2, textOffset + cornerRadius);
        _mc.bshadow.curveTo(xOffset + 0 - textWidth / 2, textOffset, xOffset + 0 - textWidth / 2 + cornerRadius, textOffset);
        _mc.bshadow.lineTo(xOffset + 0 - textWidth / 2 + cornerRadius, textOffset);
            
        _mc.bshadow.endFill();

    }

    //
    //
    public static function showTag (theText:String):Void {    
        if (!_mc) return;

        if (theText.length > 0 && theText != "undefined") {
            
            showing = true;
            
            _tipText.text = theText;
            _tipText.setTextFormat(_tipFormat);
            _tipText._y = 0 - (_tipText._height + 10);
            
            _mc._x = _container._xmouse;
            _mc._y = _container._ymouse;
            
            drawTag();
            _mc._visible = true;
            _mc._alpha = 100;
            
            showTime = getTimer();
            _mc.onEnterFrame = function ():Void {
                Tagtip.followMouse();
            }
            
        } else {
            
            hideTag();
            
        }
        
    }

    //
    //
    public static  function hideTag ():Void {

        if (!_mc) return;
        
        showing = false;
        _mc._visible = false;
        _mc._alpha = 100;
        delete _mc.onEnterFrame;

    }
    
    //
    //
    public static function followMouse ():Void {
        
        if (!_mc) return;
        
        _mc._x = _container._xmouse >> 0;
        _mc._y = (_container._ymouse + heightOffset) >> 0;
        
        drawTag();
        
        if (getTimer() - showTime > showMaxTime) {
            _mc._alpha = Math.max(0, 100 - (((getTimer() - showTime) - showMaxTime) / 5));
            if (_mc._alpha == 0) {
                hideTag();
            }
        }
        
    }
    

}
